package utilities
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author Andrew Lau
	 */
	public class TextBox extends FlxGroup
	{
		private var sprite:FlxSprite;
		private var text:String;
		private var textBox:FlxText;
		private var box:FlxSprite = new FlxSprite(0, 0);
		private var arrow:FlxSprite = new FlxSprite(0, 0, Assets.ImgTextArrow);
		
		private var lifetime:Number;
		
		private var counter:int;
		private var previousHeight:int;
		private var increases:int;
		
		protected var _finished:Boolean;
		
		protected var _name:String;
		
		public function TextBox(name:String, sprite:FlxSprite, text:String = "", width:int = 110, lifetime:Number = 1.5)
		{
			this._name = name;
			this.sprite = sprite;
			this.text = text;
			
			textBox = new FlxText(0, 0, width);
			textBox.setFormat(null, 8, 0xffffff, "center");
			
			arrow = new FlxSprite(sprite.x + 5, sprite.y - 15, Assets.ImgTextArrow);
			
			box = new FlxSprite(arrow.x + 8 - 110 / 2, arrow.y - 9 - 20 / 2 - increases * 5);
			box.makeGraphic(width, 20, 0xaa000000);
			
			this.lifetime = lifetime;
			
			counter = 0;
			previousHeight = textBox.height;
			increases = 0;
			
			_finished = false;
			
			add(box);
			add(arrow);
			add(textBox);
			
			exists = false;
		}
		
		override public function update():void
		{
			super.update();
			
			if (textBox.height != previousHeight)
			{
				previousHeight = textBox.height;
				box.makeGraphic(110, box.height + 10, 0xaa000000);
				increases++;
			}
			
			arrow.x = sprite.x + 5;
			arrow.y = sprite.y - 14;
			
			box.x = arrow.x + 8 - box.width / 2;
			box.y = arrow.y - 9 - box.height / 2 - increases * 5;
			
			if (box.x < 0)
				box.x = 0;
			else if (box.x > FlxG.width)
				box.x = FlxG.width;
			
			if (box.y < 0)
				box.y = 0;
			else if (box.y > FlxG.height - box.height)
				box.y = FlxG.height - box.height;
			
			textBox.x = box.x + box.width / 2 - textBox.width / 2;
			textBox.y = box.y + box.height / 2 - textBox.height / 2;
			
			counter++;
			if (counter <= text.length * 2)
			{
				textBox.text = text.substring(0, counter / 2);
			}
			else if (counter > text.length * 2 * lifetime)
			{
				textBox.visible = false;
				box.visible = false;
				arrow.visible = false;
				_finished = true;
				exists = false;
			}
		}
		
		override public function destroy():void
		{
			textBox.destroy();
			box.destroy();
			arrow.destroy();
			
			super.destroy();
		}
		
		public function init():void
		{
			counter = 0;
			previousHeight = textBox.height;
			increases = 0;
			
			textBox.visible = true;
			box.visible = true;
			arrow.visible = true;
			
			_finished = false;
			exists = true;
			
			arrow.x = sprite.x + 5;
			arrow.y = sprite.y - 14;
			
			box.x = arrow.x + 8 - box.width / 2;
			box.y = arrow.y - 9 - box.height / 2 - increases * 5;
		}
		
		public function get finished():Boolean
		{
			return _finished;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function setText(newText:String):void
		{
			text = newText;
		}
	}

}